package com.apps.ftapp.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.apps.ftapp.R;
import com.apps.ftapp.db.Singleton;
import com.apps.ftapp.model.StResultModel;
import com.apps.ftapp.ui.ExamActivity;
import com.apps.ftapp.util.ActivitySkipUtil;
import com.apps.ftapp.util.Async;
import com.apps.ftapp.util.Constant;
import com.apps.ftapp.util.L;
import com.apps.ftapp.util.Tools;
import com.google.gson.Gson;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.GridLabelRenderer;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import cz.msebera.android.httpclient.Header;

/**
 * 真题测试结果
 */
public class MyStSucResultActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.score)
    TextView score;
    @BindView(R.id.max_score)
    TextView maxScore;
    @BindView(R.id.max_score_num)
    TextView maxScoreNum;
    @BindView(R.id.avg_score)
    TextView avgScore;
    @BindView(R.id.avg_score_num)
    TextView avgScoreNum;
    @BindView(R.id.other_score)
    TextView otherScore;
    @BindView(R.id.other_score_num)
    TextView otherScoreNum;
    @BindView(R.id.top)
    RelativeLayout top;
    @BindView(R.id.shhijuan)
    TextView shhijuan;
    @BindView(R.id.shijian)
    TextView shijian;
    @BindView(R.id.yongshi)
    TextView yongshi;
    @BindView(R.id.ti_c)
    LinearLayout tiC;
    @BindView(R.id.adsdf)
    TextView adsdf;
    @BindView(R.id.cuotijiexi)
    TextView cuotijiexi;
    @BindView(R.id.quanbujiexi)
    TextView quanbujiexi;
    @BindView(R.id.graph)
    GraphView graph;

    private String paperResultId;


    private Gson gson = new Gson();

    private String id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_st_suc_result);
        ButterKnife.bind(this);

        setTitle("考试结果");


        //新页面接收数据
        Bundle bundle = this.getIntent().getExtras();
        //接收name值
        id = bundle.getString("id");


        cuotijiexi.setOnClickListener(this);

        quanbujiexi.setOnClickListener(this);


        tijiao();
    }


    public void tijiao() {


        Singleton s = Singleton.getInstance();
        L.i("main", s.getStartTime());
        L.i("main", Tools.getTime());
        L.i("main", s.getId());
        final String cha = Tools.getcha(s.getStartTime(), s.getEndTime());
        RequestParams params = new RequestParams();
        params.put("token", Constant.token);
        params.put("userId", Constant.uid);
        params.put("id", id);
        Async.post("test/result", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if (statusCode == 200) {
                    L.i("main", new String(responseBody));
                    StResultModel result = gson.fromJson(new String(responseBody), StResultModel.class);

                    score.setText(result.getPaperResult().getScore());
                    maxScoreNum.setText(result.getMax_score() + "");
                    avgScoreNum.setText(result.getAverage() + "");//+ "分"
                    otherScoreNum.setText(result.getBeat());


                    //用时
//                    yongshi.setText(cha);


//                    String time = result.getPaperResult().getBeginTime()+ "至" + result.getPaperResult().getEndTime();
                    String time = result.getPaperResult().getBeginTime();//+ "至" + result.getPaperResult().getEndTime();
                    shhijuan.setText("模考试卷：" + Tools.delHTMLTag(result.getPaperResult().getPaperName()));
//                        shijian.setText("模考时间："+ DateUtil.timeStamp2Date(Tools.isNull(result.getResults().getBeginTime()),"yyyy-MM-dd hh:mm:ss"))+"至"+DateUtil.timeStamp2Date(Tools.isNull(result.getResults().getEndTime()),"yyyy-MM-dd hh:mm:ss")));

                    shijian.setText("模考时间：" + time);
                    yongshi.setText("考试用时：" + cha);
//                        ActivitySkipUtil.skipAnotherActivity(GestureFilpActivity.this,SubjectResultActivity.class,true);


                    paperResultId = result.getPaperResult().getId();
//
//                    GraphView graph = (GraphView) findViewById(R.id.graph);
//                    LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[] {
//                            new DataPoint(0, 1),
//                            new DataPoint(1, 5),
//                            new DataPoint(2, 3),
//                            new DataPoint(3, 2),
//                            new DataPoint(4, 6)
//                    });
//
//                    series.setColor(Color.parseColor("#01c475"));
//                    series.setTitle("Random Curve 1");
////                    series.setColor(Color.GREEN);
//                    series.setDrawDataPoints(true);
//                    series.setDataPointsRadius(1);
//                    series.setThickness(2);
//
//                    graph.addSeries(series);

                    // custom paint to make a dotted line
//                    Paint paint = new Paint();
//                    paint.setStyle(Paint.Style.FILL_AND_STROKE);
//                    paint.setStrokeWidth(1);
//                    paint.setPathEffect(new DashPathEffect(new float[]{8, 5}, 0));
//                    series.setCustomPaint(paint);

                    LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>();
                    if (null != result.getHzfenduan() && result.getHzfenduan().size() > 0) {
//                        DataPoint[] dataPoints = new DataPoint[200];
                        for (int k = 0; k < result.getHzfenduan().size(); k++) {
//                            dataPoints[k]=new DataPoint(result.getHzfenduan().get(k), result.getSzfenduan().get(k));
                            series.appendData(new DataPoint(result.getHzfenduan().get(k), result.getSzfenduan().get(k)), false, result.getHzfenduan().size());
                        }
//                        series.resetData(dataPoints);
                    }

                    series.setColor(Color.parseColor("#01c475"));
                    series.setTitle("Random Curve 1");
//                    series.setColor(Color.GREEN);
                    series.setDrawDataPoints(true);
                    series.setDataPointsRadius(1);
                    series.setThickness(3);
                    graph.getGridLabelRenderer().setGridStyle(GridLabelRenderer.GridStyle.HORIZONTAL);//表格样式，水平线
//                    graph.getGridLabelRenderer().reloadStyles();
//                    graph.getGridLabelRenderer().setHorizontalLabelsAngle(120);
                    graph.getViewport().setBackgroundColor(Color.argb(255, 255, 255, 255));
//                    graph.getViewport().setDrawBorder(true);
//                    Paint paint = new Paint();
//                    paint.setStyle(Paint.Style.STROKE);
//                    paint.setStrokeWidth(2);
//                    paint.setColor(Color.parseColor("#01c475"));
//                    paint.setPathEffect(new DashPathEffect(new float[]{8, 5}, 0));
//                    series.setCustomPaint(paint);
//                    graph.getViewport().setBorderPaint(Paint);
                    graph.getGridLabelRenderer().setVerticalAxisTitle("百分比");
//                    graph.getGridLabelRenderer().setHorizontalLabelsColor(Color.parseColor("#01c475"));//水平轴字体颜色
//                    graph.getGridLabelRenderer().setVerticalLabelsColor(Color.parseColor("#01c475"));//垂直轴字颜色
                    graph.getGridLabelRenderer().setHorizontalAxisTitle("分");
                    graph.addSeries(series);

//
//                    series.setBackgroundColor(R.color.white);
////                    result.getPaperResult().getd
//                    graph.addSeries(series);
//
//                    //设置XY轴的下标签
//                    StaticLabelsFormatter formatter = new StaticLabelsFormatter(graph);
////                    formatter.setHorizontalLabels(new String[]{"0", "20", "40", "3月", "4月"});
//                    formatter.setHorizontalLabels(new String[]{"0", "20", "40", "60","80","100"});
//                    graph.getGridLabelRenderer().setLabelFormatter(formatter);
//                    //设置折线颜色
//                    series.setColor(Color.RED);

//
//                    LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[] {
//                            new DataPoint(0, 1),
//                            new DataPoint(1, 5),
//                            new DataPoint(2, -1),
//                            new DataPoint(3, 10),
//                            new DataPoint(4, 6),
//                            new DataPoint(5, 8),
//                            new DataPoint(4, 5)
//                    });
//                    graph.addSeries(series);


                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cuotijiexi:
                HashMap map1 = new HashMap();
                map1.put("id", paperResultId);
//                ActivitySkipUtil.skipAnotherActivity(MyStSucResultActivity.this, CuotiJieXiActivity.class, map1);
                ActivitySkipUtil.skipAnotherActivity(MyStSucResultActivity.this, StErrorJxActivity.class, map1);

                break;

            case R.id.quanbujiexi:
                //全部错题
                HashMap map = new HashMap();
                map.put("id", paperResultId);
//                ActivitySkipUtil.skipAnotherActivity(MyStSucResultActivity.this, CuotiAllActivity.class, map);

                ActivitySkipUtil.skipAnotherActivity(MyStSucResultActivity.this, CuotiAllJxActivity.class, map);

                break;

            case R.id.jixudati:


                HashMap<String, Object> data = new HashMap<String, Object>();
                data.put("id", id);
//                data.put("keys",keys);
                ActivitySkipUtil.skipAnotherActivity(MyStSucResultActivity.this, ExamActivity.class, data);

                break;
        }
    }

}
